<?php
namespace Api\Service;
use Think\Model;
class ApiService extends Model {
	
	private $m;	
	function __construct(){
		//$this->m = D('YgclTerm');
	}
	
	/**
	 * 短信发送基方法
	 */
	public function _sms($mobile,$tplkey,$params){
		if(!$mobile){
			_apiReturn(0,'手机号码不能为空');
		}
		if(!$tplkey){
			_apiReturn(0,'短信模板名不能为空');
		}
		$alidayu = new \Users\Lib\Alidayu\SendSMS(C('ALIDAYU_TPLS'));
		$result = $alidayu->send($mobile,$tplkey,$params);
		if($result == -1){
			return 'the sms tpl is not exist';
		}
	
		if($result['result']['success']){
			return 1;
		}else{
			return 'send sms error';
		}
	}
	
	//发送短信
	public function _send_sms($mobile,$code,$type){
		//$content = "验证码为：".$code;
		//$mobileservice = new \Users\Service\MobileService('hhq110@126.com','haiqin880709');
		//$result = $mobileservice->sendSMS($mobile, $content, intval($mobile).microtime());
		//发送短信
		
		$params = '{"code":"'.$code.'"}';
		$data = $this->_sms($mobile,'login',$params);
		if($data === 1){
			$status = 1;
		}else{
			$status = 2;
		}
		$m = D('Sms');
		$m->where(array('mobile'=>$mobile,'type'=>$type))->delete();
		$data = array(
				'mobile' => $mobile,
				'code'   => $code,
				'type'   => $type,
				'status' => $status,
				'hasuse' => 2,
				'token_time' => time()+60*30, //有效期30分钟
				'create_time' => Date('Y-m-d H:i:s'),
		);
		if($m->add($data)){
			return true;
		}else{
			return false;
		}
	}
   
}